#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")

//int main() {

    WSADATA wsaData;

    int iResult;

    // Initialize Winsock
    iResult = WSAStartup( MAKEWORD(2,2), &wsaData );

    if(iResult != 0) {
        printf( "WSAStartup failed: %d\n", iResult );
        return 1;
    }

    struct addrinfo *result = NULL, *ptr = NULL, hints;

    // ZeroMemory() wypełnia blok pamięci hints zerami
    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    #define DEFAULT_PORT "27015"
    // Resolve the local address and port to be used by the server
    iResult = getaddrinfo( NULL, DEFAULT_PORT, &hints, &result );
    if ( iResult != 0 ) {
        printf( "getaddrinfo failed: %d\n", iResult );    //%d specyfikator formatu funkcji printf()
        WSACleanup();                                   // liczba całkowita ze znakiem w sys dziesiętnym
        return 1;
    }

    SOCKET ListenSocket = INVALID_SOCKET;

    // Create a SOCKET for the server to listen for client connections
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);


    if ( ListenSocket == INVALID_SOCKET ) {
        printf( "Error at socket(): %ld\n", WSAGetLastError() );
        freeaddrinfo( result );
        WSACleanup();
        return 1;
    }


    // Setup the TCP listening socket
    iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen );
    if ( iResult == SOCKET_ERROR ) {
        printf( "bind failed with error: %d\n", WSAGetLastError() );
        freeaddrinfo( result );
        closesocket( ListenSocket );
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    if ( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR ) {
        printf( "Listen failed with error: %ld\n", WSAGetLastError() );
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    SOCKET ClientSocket;

    ClientSocket = INVALID_SOCKET;

    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if(ClientSocket == INVALID_SOCKET) {
        printf("accept failed: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    /*** Receiving and Sending Data on the Server ***/



//    return 0;
//}
